define(['jquery', 'echarts'], function($) {
    'use strict';
    /*set echarts*/
    var option = {
        title: {
            text: '',
            subtext: ''
        },
        tooltip: {
            trigger: 'axis'
        },
        legend: {
            x: 'center',
            data: ['']
        },
        calculable: true,
        polar: [{
            indicator: [{
                text: '科研规模',
                max: 10
            }, {
                text: '科研质量',
                max: 10
            }, {
                text: '顶尖论文',
                max: 10
            }, {
                text: '顶尖人才',
                max: 10
            }],
            name: {
                textStyle: {
                    color: '#B8C3CC'
                }
            },
            center: ['50%', 200],
            radius: 110
        }],
        series: [{
            type: 'radar',
            tooltip: {
                trigger: 'item'
            },
            itemStyle: {
                normal: {
                    areaStyle: {
                        type: 'default'
                    }
                }
            },
            data: [{
                value: [60, 73, 85, 40],
                name: ''
            }]
        }, ]
    };
    var getStateParams = (function() {
        var sSeach = location.search.split('?')[1];
        var json = {};
        if (sSeach) {
            var arr = sSeach.split('&');
            for (var i = 0; i < arr.length; i++) {
                var aTmp = arr[i].split('=');
                json[aTmp[0]] = aTmp[1];
            }
        }
        return json;
    })();

    function isObject(value) {
        return value !== null && typeof value === 'object';
    }

    $(function() {
        var college = decodeURIComponent(getStateParams.college);
        $('.list-left .text').text(college + ' — 学科排行');
        //中医药大学－学科排行
        $('.subject').text(college);
        /*获取高校学科排行*/
        var param = {
            college: college
        };
        $.get("/med/subject/poper/college/subject", param).then(function(response) {
            if (response.code == 200) {
                var data = response.message;
                if (data.length) {
                    /*渲染左侧列表*/
                    var aTrs = [];
                    data.forEach(function(n, index) {
                        var sTr = ["<tr data-score =" + n.score + "  data-sc=" + JSON.stringify(n.sc) + "  class=" + (index == 0 ? 'active' : '') + ">",
                            "       <td>" + n.cname + "</td>",
                            "       <td>" + n.score.toFixed(2) + '分' + "</td>",
                            "</tr>"
                        ].join("");
                        aTrs.push(sTr);
                    });
                    var sLeftTabel = ["<table class=\"m-table\">",
                        "  <thead>",
                        "   <tr>",
                        "      <th class=\"cola\">名称</th>",
                        "      <th>综合评分</th>",
                        "   </tr>",
                        "  </thead>",
                        "  <tbody>" + aTrs.join('') + "</tbody>",
                        "</table>"
                    ].join("");
                    $('.list-left').append(sLeftTabel);
                }
            }
            return data;
        }).then(function(response) {
            /*获取学科关键词排行*/
            var showScore = function(list, isFirst) {
                /*set title*/
                $('.list-right .title').text('综合评分：' + ((list.score * 1).toFixed(2) || '0.00') + ' 分');
                /*第一次加载显示*/
                if (isFirst) {
                    var loding = [
                        " <table class=\"m-table\">",
                        "    <thead>",
                        "        <tr>",
                        "          <th>正在加载中。。。</th>",
                        "        </tr>",
                        "    </thead>",
                        "</table>",
                    ].join("");
                    $('.list-right').append(loding);
                }
                //
                var sc = isObject(list.sc) ? list.sc : JSON.parse(list.sc)
                option.series[0].data[0].value = [sc.src, sc.srq, sc.tp, sc.tt]; // 加载数据到data中
                var myChart = echarts.init(document.getElementById('charts'));
                myChart.setOption(option, true); //为echarts对象加载数据
                $(".list-right table").remove();
            }

            /*默认显示*/
            showScore(response[0], true);
            /*点击左侧列表更新右侧列表数据*/
            $('.list-left tbody tr').mouseover(function(e) {
                //高亮显示
                $(this).addClass('active').siblings().removeClass('active');
                var score = this.getAttribute("data-score");
                var sc = this.getAttribute("data-sc");
                showScore({
                    score: score,
                    sc: sc,
                });
            });
        });
    });
});
